clear all
global system "linux"

if "${system}" == "linux" {
	global code "/"
	global s "/"
}

run "${code}${s}_set-path.do"
cd "${code}/tba_elig"

*** 0. Specifications

args index 

cap log close _all
log using "${log}/log-jc-`index'", name("log`index'") text replace

local MaxStage = 3
local MaxWindow = 3
local MaxNumPoly = 3
local MaxRobust = 6

local Stage = ceil(`index'/ `MaxWindow'/ `MaxNumPoly'/ `MaxRobust')

local index1 = `index' - (`Stage' - 1)* `MaxWindow'* `MaxNumPoly'* `MaxRobust'
local WindowType = ceil(`index1'/ `MaxNumPoly'/ `MaxRobust')

if `WindowType' == 1 local Window "abs(PaboveC) <= 50"
if `WindowType' == 2 local Window "abs(PaboveC) <= 100"
if `WindowType' == 3 local Window "abs(PaboveC) <= 150"

local index2 = `index1' - (`WindowType' - 1)* `MaxNumPoly'* `MaxRobust'
local NumPoly = ceil(`index2'/ `MaxRobust')

local index3 = `index2' - (`NumPoly' - 1)* `MaxRobust'
local RobustType = `index3'-1

*** 1. actual analysis starts here


use "${temp}/embs-lps-matched.dta", clear  // 12c-embs-lps-match.do


// specifications
do "_embs-lps-spec.do"

keep if `Window'

if `RobustType' == 1 drop if year == 2010 
if `RobustType' == 2 keep if year == 2012 // keep only freddie


local iv "notba"

if `RobustType' == 3 {
	keep if notba == 0
	gen jc = orig_loan_amount > 417000
	local iv "jc"
}

if `RobustType' == 4 { // same sample as LTV analysis
	keep if year ==2012
	qui su credit_score, d
	keep if credit_score <= `r(p50)'
}

local SE "vce(robust)"
if `RobustType' == 5 {
	local SE "cluster(prop_zip_code)"
}
if `RobustType' == 6 {
	local SE "cluster(prop_zip_code seller_id_id)"
}

global poly1 "PaboveC 1.HiP#c.PaboveC"
global poly2 "$poly1 c.PaboveC#c.PaboveC 1.HiP#c.PaboveC#c.PaboveC"
global poly3 "$poly2 c.PaboveC#c.PaboveC#c.PaboveC 1.HiP#c.PaboveC#c.PaboveC#c.PaboveC"



if `Stage' == 1 {
	reghdfe `iv' ${poly`NumPoly'} $controls, a(${FEs}) vce(robust)
}
if `Stage' == 2 {
	reghdfe orig_rate  ${poly`NumPoly'} $controls, a(${FEs}) vce(robust)
}
if `Stage' == 3 {
	
	global poly1 ""
	global poly2 "c.PaboveC#c.PaboveC 1.HiP#c.PaboveC#c.PaboveC"
	global poly3 "$poly2 c.PaboveC#c.PaboveC#c.PaboveC 1.HiP#c.PaboveC#c.PaboveC#c.PaboveC"

	reghdfe orig_rate PaboveC ${poly`NumPoly'} $controls (`iv'= slope), a(${FEs}) `SE'
}

estimate save "${Estimates}/embs-lps-Stage`Stage'-Window`WindowType'-Poly`NumPoly'-Robust`RobustType'", replace

cap log close _all



// Figure
if inlist(`Stage', 1, 2)  & `WindowType' == 3 & `NumPoly' == 3 & inlist(`RobustType', 0, 3, 4) {

preserve
	predict resid, r 
	
	local Poly ${poly`NumPoly'}
	foreach z in `Poly' {
		replace resid = resid + _b[`z']* (`z')
	}
	local DepVar = e(depvar)

	qui su `DepVar' if HiP == 0
	replace resid = resid + `r(mean)'
	
	gen bin = ceil(PaboveC/2.5)*2.5
	label var bin "Appraisal Value - 1.25 CLL ($1000)"
	collapse resid, by(bin) fast
	twoway scatter resid bin, xline(0) xlabel(-150(50)150) ytitle("") ylabel(, angle(0)) 
	
	local name ""
	if `RobustType' == 3 local name "-TBAonly"
	if `RobustType' == 4 local name "-lowcredit_score"
	
	graph export "${figure}/rdplot-embs-lps-`DepVar'`name'.pdf", replace
	
restore

}

